﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text;

namespace lcs.Web.supply
{
    public partial class ListTempskd : System.Web.UI.Page
    {
        Maticsoft.BLL.monthSupplayPlan msm = new Maticsoft.BLL.monthSupplayPlan();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["login"] != null)
                {
                    DataSet ds = Session["login"] as DataSet;

                    int roles_id = Convert.ToInt32(ds.Tables[0].Rows[0]["roles_id"].ToString());
                    int user_id = Convert.ToInt32(ds.Tables[0].Rows[0]["user_id"].ToString());

                    Bind(user_id);
                }
                //Bind(11);
            }
        }

        private void Bind(int user_id)
        {
            StringBuilder str = new StringBuilder();

            str.Append(" user_id=" + user_id + " and status = '1' ");

            DataSet ds = msm.GetListMonthSupplyInfo(str.ToString());

            DataTable dt = ds.Tables[0];

            DataTable dtSource = dt.Clone();

            List<lcs.Model.configinfo> cm = new lcs.BLL.configinfo().GetModelList("");

            if (cm.Count == 1)
            {
                lcs.Model.configinfo c = cm[0];

                foreach (DataRow r in dt.Rows)
                {

                    DateTime supplyDate = DateTime.Parse(r["supplyDate"].ToString());//供应年月
                    DateTime monthEndTime;//月供应计划结束时间
                    DateTime monthStartTime;//月供应计划起始时间

                    int day = DateTime.Today.Day;

                    if (day < c.ygyjs)
                    {
                        monthStartTime = DateTime.Parse(DateTime.Today.AddMonths(-1).ToString("yyyy-MM-" + c.ygyqs + ""));
                        monthEndTime = DateTime.Parse(DateTime.Today.ToString("yyyy-MM-" + c.ygyjs + ""));
                    }
                    else
                    {
                        monthStartTime = DateTime.Parse(DateTime.Today.ToString("yyyy-MM-" + c.ygyqs + ""));
                        monthEndTime = DateTime.Parse(DateTime.Today.AddMonths(1).ToString("yyyy-MM-" + c.ygyjs + ""));
                    }

                    if (DateTime.Compare(supplyDate, monthEndTime) < 0 && DateTime.Compare(supplyDate, monthStartTime) > 0)//供应年月大于起始时间，且小于结束时间
                    {
                        dtSource.ImportRow(r);
                    }

                }


                this.lbl_se.Text = "*注：当月供应计划起始日期：本月" + c.ygyqs + "号，截止日期：下月" + c.ygyjs + "号";
            }


            this.GridView1.DataSource = dtSource;
            this.GridView1.DataBind();

        }

        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "lbn_confirmFrist")
            {
                string monthSupplyPlanId = e.CommandArgument.ToString();

                if (Session["login"] != null)
                {
                    DataSet ds_login = Session["login"] as DataSet;

                    int roles_id = Convert.ToInt32(ds_login.Tables[0].Rows[0]["roles_id"].ToString());
                    int user_id = Convert.ToInt32(ds_login.Tables[0].Rows[0]["user_id"].ToString());

                    string strWhere = " supplyPlanUser= " + user_id + " and monthSupplyPlanId=" + monthSupplyPlanId;

                    DataSet ds = msm.GetList(strWhere);//当前登录人Id和月计划ID

                    lcs.BLL.tempskd tm = new BLL.tempskd();//临时质量计划表

                    lcs.Model.tempskd t = new Model.tempskd();
                    t.material = ds.Tables[0].Rows[0]["material"].ToString();
                    t.monthSupplyPlanId = int.Parse(monthSupplyPlanId);
                    t.project = ds.Tables[0].Rows[0]["projectName"].ToString();
                    t.tdate = DateTime.Now;
                    t.ttype = "临时质量计划";
                    

                    if (tm.Add(t) > 0)
                    {
                        Maticsoft.Common.MessageBox.Show(this, "添加发货期质量确认成功！");

                        Bind(user_id);
                    }
                }


            }
        }

        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            this.GridView1.PageIndex = e.NewPageIndex;

            if (Session["login"] != null)
            {
                DataSet ds = Session["login"] as DataSet;

                int roles_id = Convert.ToInt32(ds.Tables[0].Rows[0]["roles_id"].ToString());
                int user_id = Convert.ToInt32(ds.Tables[0].Rows[0]["user_id"].ToString());

                Bind(user_id);
            }

        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)//判定当前的行是否属于datarow类型的行
            {
                //当鼠标放上去的时候 先保存当前行的背景颜色 并设置新的背景色
                e.Row.Attributes.Add("onmouseover", "onmouseroverop(this)");
                //当鼠标离开的时候 将背景颜色恢复成之前的颜色
                e.Row.Attributes.Add("onmouseout", "onmouseroutop(this)");
                //单击行改变行背景颜色
                e.Row.Attributes.Add("onclick", "ItemOver(this)");
            }
        }

        //protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        //{
        //    if (e.Row.RowType == DataControlRowType.DataRow)
        //    {
        //        Label lbl_supplyDate = e.Row.FindControl("lbl_supplyDate") as Label;//供应年月

        //        List<lcs.Model.configinfo> cm = new lcs.BLL.configinfo().GetModelList("");

        //        if (cm.Count == 1)
        //        {
        //            lcs.Model.configinfo c = cm[0];

        //            //string monthStartTime = DateTime.Today.AddMonths(-1).ToString("yyyy-MM-" + c.ygyqs + "");//月供应计划起始时间
        //            //string monthEndTime = DateTime.Today.ToString("yyyy-MM-" + c.ygyjs + "");//月供应计划结束时间

        //            if (!string.IsNullOrEmpty(lbl_supplyDate.Text))
        //            {
        //                DateTime supplyDate = DateTime.Parse(lbl_supplyDate.Text);//供应年月
        //                DateTime monthEndTime;//月供应计划结束时间
        //                DateTime monthStartTime;//月供应计划起始时间


        //                int day = DateTime.Today.Day;

        //                if (day < c.ygyjs)
        //                {
        //                    monthStartTime = DateTime.Parse(DateTime.Today.AddMonths(-1).ToString("yyyy-MM-" + c.ygyqs + ""));
        //                    monthEndTime = DateTime.Parse(DateTime.Today.ToString("yyyy-MM-" + c.ygyjs + ""));
        //                }
        //                else
        //                {
        //                    monthStartTime = DateTime.Parse(DateTime.Today.ToString("yyyy-MM-" + c.ygyqs + ""));
        //                    monthEndTime = DateTime.Parse(DateTime.Today.AddMonths(1).ToString("yyyy-MM-" + c.ygyjs + ""));
        //                }


        //                if (DateTime.Compare(supplyDate, monthEndTime) < 0 && DateTime.Compare(supplyDate, monthStartTime) > 0)//供应年月大于起始时间，且小于结束时间
        //                {
        //                    e.Row.Visible = true;
        //                }
        //                else
        //                {
        //                    e.Row.Visible = false;
        //                }

        //            }

        //        }
        //    }
        //}

    }
}